package com.glory.cqaospda.view

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.view.KeyEvent
import android.view.View
import android.widget.EditText
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.glory.cqaospda.R
import com.glory.cqaospda.adapter.SpareParesLifeAdapter
import com.glory.cqaospda.base.Constant
import com.glory.cqaospda.base.MyApp
import com.glory.cqaospda.bean.QueryRaptsInfoBean
import com.glory.cqaospda.utils.EditTextEnterUtils
import com.glory.cqaospda.viewmodel.QuerySparePartsLifeModel
import com.glory.mvvmnet.base.BaseActivity
import com.glory.mvvmnet.entity.ToastMessageBean
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_EditTextUtils
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText
import kotlinx.android.synthetic.main.activity_querysparepartslife.*
import kotlinx.android.synthetic.main.activity_title.*
import java.lang.Exception

class QuerySparePartsLifeActivity : BaseActivity<QuerySparePartsLifeModel>() {

    var spareParesLifeAdapter:SpareParesLifeAdapter? = null
    var handler: Handler = object : Handler() {
        @SuppressLint("HandlerLeak")
        override fun handleMessage(msg: Message) {
            when(msg.what) {
                0x10 -> {
                    spareParesLifeAdapter!!.data.clear()
                    var s: String = msg.obj as String
                    val intent = Intent()
                    intent.putExtra("type", "Error")
                    intent.putExtra("error", s)
                    val clazz = Class.forName("com.glory.cqaospda.view.SkipDialogActivity")
                    intent.setClass(this@QuerySparePartsLifeActivity, clazz)
                    startActivity(intent)
                }
            }
            super.handleMessage(msg)
        }
    }

    override fun initData() {
        defaultQuerySparePartsLife(etEqpId)

        viewModel.defUI.callObserve.observe(this@QuerySparePartsLifeActivity) {
            if (!Constant.okgoError) {
                when (it.type) {
                    1 -> {
                        val queryRaptsInfoBean = it.item as QueryRaptsInfoBean
                        spareParesLifeAdapter!!.setList(queryRaptsInfoBean.mPartsList)
                    }
                    2 ->{
                        spareParesLifeAdapter!!.data.clear()
                    }
                }
            }else{
                Constant.okgoError = false
            }
        }
        btQuery.setOnClickListener{
            if (etEqpId.text.toString() == ""){
                viewModel.defUI.toastEvent.value = ToastMessageBean("设备号不能为空",false)
            }
            spareParesLifeAdapter!!.data.clear()
            if (Constant.isFastClick()) {
                viewModel.defaultQuerySparePartsLife(etEqpId.text.toString())
            }
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        tvTitle.text = "备件寿命查询"
        tvBaseSubmit.visibility = View.GONE
        tvUserName.text = Constant.UserName
        initRecyclerView(rvEqpInfoList,this)
        spareParesLifeAdapter = SpareParesLifeAdapter(null)
        rvEqpInfoList.adapter = spareParesLifeAdapter

        val instance = MyApp.instance
        instance!!.handler = handler
    }

    override fun layoutId(): Int {
        return R.layout.activity_querysparepartslife
    }

    private fun defaultQuerySparePartsLife(
        editText: FreeUI_ClearEditText?,
    ) {
        EditTextEnterUtils.initEnterToUpperCase(editText)
        FreeApi_EditTextUtils.setEditTextListener(editText, this) { s: String? ->
            if (etEqpId.text.toString().equals("")) {
                viewModel.defUI.toastEvent.value = ToastMessageBean("请扫描设备ID", true)
                return@setEditTextListener
            }
            spareParesLifeAdapter!!.data.clear()
            viewModel.defaultQuerySparePartsLife(s!!)
        }
    }

    protected fun initRecyclerView(recyclerView: RecyclerView, context: Context?) {
        recyclerView.layoutManager = LinearLayoutManager(context)
        recyclerView.itemAnimator = DefaultItemAnimator()
        recyclerView.addItemDecoration(DividerItemDecoration(context, 1))
    }

    @SuppressLint("SetTextI18n")
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (requestCode == 1) {
            when (resultCode) {
                108 -> {
                    finish()
                }
            }
        }
    }
    override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK){
            intent.putExtra("type", "KeyEvent")
            intent.setClass(this, SkipDialogActivity::class.java)
            startActivityForResult(intent, 1)
            return false
        }
        return super.onKeyUp(keyCode, event)
    }
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == 520) {
            try {
                val view = window.decorView.findFocus() as EditText
                when (view) {
                    etEqpId ->{
                        etEqpId.setText("")
                    }
                }
            }catch (e: Exception){
                println(e.message)
            }
        }
        return super.onKeyDown(keyCode, event)
    }
}